<template>
  <div>
    <van-nav-bar
      title="编辑地址"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />

    <van-address-edit
      show-postal
      show-delete
      show-set-default
      :area-list="areaList"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      :address-info="addressInfo"
      @save="add"
      @delete="del"
      ref="地址"
    />
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { post } from "@/request/index";
export default {
  data() {
    return {
      // 省市区
      areaList,

      // 初始地址值
      addressInfo: this.$route.query,
      //  {
      // id: 111,
      // name: "猪猪侠",
      // tel: "15975088150",
      // province: "广东省",
      // city: "深圳市",
      // county: "龙岗区",
      // addressDetail: "布吉小花园1区1巷1号101",
      // areaCode: "518112",
      // postalCode: "518112",
      // isDefault: "false",
      // },
    };
  },
  create() {},
  methods: {
    // 点击保存按钮

    add(val) {
      post("/address/edit", val)
        .then((res) => {
          console.log(res);
          this.$router.back();
        })
        .catch((err) => console.log("修改失败"));
    },
    del(val) {
      post("/address/del", { addressId: this.addressInfo.addressId })
        .then((res) => {
          this.$router.back();
        })
        .catch((err) => console.log("删除失败"));
    },
    // 修改详细地址触发
    // onChangeDetail(val) {
    //   // 防抖
    //   let timer;
    //   clearTimeout(timer);
    //   setTimeout(() => {
    //     this.addressInfo.addressDetail = val;
    //     console.log("详细地址", this.addressInfo.addressDetail);
    //   }, 500);
    // },
    // // 修改地区时触发，返回数组，省，市，区
    // onChangeArea(val) {
    //   this.addressInfo.province = val[0].name;
    //   this.addressInfo.city = val[1].name;
    //   this.addressInfo.county = val[2].name;
    //   this.addressInfo.postalCode = val[2].code;
    //   this.addressInfo.areaCode = val[2].code;
    //   console.log("省市区", val);
    // },
    // a(addressDetail) {
    //   console.log(this.$refs[addressDetail]);
    // },
    // onChangeDefalut(val) {
    //   this.addressInfo.isDefault = val;
    // },
  },
};
</script>

<style>
</style>